import * as React from 'react';
import {
    fetchUtils, Admin, Resource, ListGuesser
} from 'react-admin';
// import jsonServerProvider from 'ra-data-json-server';
import restProvider from 'ra-data-simple-rest';
import polyglotI18nProvider from 'ra-i18n-polyglot';

// import cookie from 'react-cookies'

import { ResourceGuesser, MyListGuesser } from './custom/resourceGuesser';
import Dashboard from './dashbord';
import authProvider from './authProvider';
import config from './config';
import myMessages from './i18n';
import customRoutes from './custom/routes';

import {
    DeviceModel,
    GatewayModel,
    HeartRateDetailModel,
    SleepDetailModel,
    TemperatureModel,
    HeartRateMeasureModel,
    HRVModel,
    StepDetailModel,
    StepPerDayModel
} from './models'

import {
    QRBulkActionButtons,
    scanLogExporter,
    QRGenButton,
    CustomDateFilter
} from './custom/components'

const fetchJson = (url, options = {}) => {
    if (!options.headers) {
        options.headers = new Headers({ Accept: 'application/json' });
    }
    // add your own headers here
    // options.headers.set('Cookie', 'token=' + cookie.load('token'));
    options.headers.set('Token', localStorage.getItem('token'));
    // console.log(options.headers)
    return fetchUtils.fetchJson(url, options);
}

console.log(myMessages)

myMessages.csv.buttonMain.label = '导入'

const i18nProvider = polyglotI18nProvider(() => myMessages, 'en');

const Device = new ResourceGuesser(DeviceModel, {
    title: '手环列表',
    
})

const Gateway = new ResourceGuesser(GatewayModel, {
    title: '网关列表',
})
const HeartRateDetail = new ResourceGuesser(HeartRateDetailModel, {
    title: '心率详情',
    customFilter: CustomDateFilter(),
})

const SleepDetail = new ResourceGuesser(SleepDetailModel, {
    title: '睡眠详情',
    customFilter: CustomDateFilter(),
})

const Temperature = new ResourceGuesser(TemperatureModel, {
    title: '体温',
    customFilter: CustomDateFilter(),
})

const HeartRateMeasure = new ResourceGuesser(HeartRateMeasureModel, {
    title: '心率测量',
    customFilter: CustomDateFilter(),
})
const HRV = new ResourceGuesser(HRVModel, {
    title: '心率详情',
    customFilter: CustomDateFilter(),
})

const StepDetail = new ResourceGuesser(StepDetailModel, {
    title: '计步详情',
    customFilter: CustomDateFilter(),
})

const StepPerDay = new ResourceGuesser(StepPerDayModel, {
    title: '每日计步',
    customFilter: CustomDateFilter(),
})


const App = () => (
    <Admin locale="en"
        customRoutes={customRoutes}
        dataProvider={restProvider(config.base_url + '/admin', fetchJson)}
        authProvider={authProvider}
        dashboard={Dashboard}
        i18nProvider={i18nProvider}
    >
        <Resource name="devs" options={{ label: '手环' }} list={Device.List} />
        <Resource name="gateway" options={{ label: '网关' }} list={Gateway.List} />
        <Resource name="hr_detail" options={{ label: '心率详情' }} list={HeartRateDetail.List} />
        <Resource name="hr_measure" options={{ label: '心率测量' }} list={HeartRateMeasure.List} />
        <Resource name="sleep_detail" options={{ label: '睡眠详情' }} list={SleepDetail.List} />
        <Resource name="temperature" options={{ label: '体温' }} list={Temperature.List} />
        <Resource name="hrv" options={{ label: 'HRV' }} list={HRV.List} />
        <Resource name="step_detail" options={{ label: '计步详情' }} list={SleepDetail.List} />
        <Resource name="step_per_day" options={{ label: '每日计步' }} list={StepPerDay.List} />
    </Admin>
);
export default App;
